﻿using DB;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TrainTicketBookingSystem
{
    internal class StationService
    {
        public List<Station> FindAllStation()
        {
            List<Station> stationsList = new List<Station>();

            string sql = " select * from station ";
            MySqlDataReader reader = MySqlUtil.ExecutQuery(sql);

            while (reader.Read()) 
            { 
                int id = reader.GetInt32("stationId");
                string name = reader.GetString("name");

                stationsList.Add(new Station(id, name));
            }

            return stationsList;
        }

        public int FindStationIdByName(string name)
        {
            string sql = " select * from station where name = @name ";
            MySqlDataReader reader = MySqlUtil.ExecutQuery(sql,new MySqlParameter("@name",name));

            if (reader.Read())
            {
                return reader.GetInt32("stationId");
            }

            return 0;
        }

        public string FindStationNameById(int id)
        {
            string sql = " select * from station where stationId = @id ";
            MySqlDataReader reader = MySqlUtil.ExecutQuery(sql, new MySqlParameter("@id", id));

            if (reader.Read())
            {
                return reader.GetString("name");
            }

            return "";
        }
    }
}
